package cn.htsyb.synergy.orm.jpa.model.param;

import cn.htsyb.synergy.orm.jpa.criterion.ExpressBuilder;
import cn.htsyb.synergy.orm.jpa.criterion.QueryExpress;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
/**
 *
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/11
 * @since 1.0
 */
@Data
@Schema(description ="实体(标准)请求参数抽象类")
public abstract class AbsStandardParam extends AbsIdStatusParam {

    @Schema(description = "创建时间周期查询，数组查询方式:[$start,$end]，只有一个值时使用EQ，格式:yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected List<Date> createdDate;

    @Schema(name = "createdBy", description ="创建者编号")
    protected String createdBy;

    @Schema(description = "修改时间-周期查询，数组查询方式:[$start,$end]，只有一个值时使用EQ，格式:yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected List<Date> modifiedDate;

    @Schema(name = "modifiedBy", description ="修改者编号")
    protected String modifiedBy;

    /**
     * 通用过滤
     */
    @Schema(name = "filter", description ="通用过滤")
    protected String filter;

    @Override
    public List<QueryExpress> searchCriterion() {
        return ExpressBuilder.instance()
                .addAllExpress(super.searchCriterion())
                .addExpressOfOperator(!StringUtils.hasLength(createdBy), "createdBy", QueryExpress.Operator.EQ, createdBy)
                .addExpressOfBetweenOrEq(!ObjectUtils.isEmpty(createdDate), "createdDate", createdDate)
                .addExpressOfOperator(!StringUtils.hasLength(modifiedBy), "modifiedBy", QueryExpress.Operator.EQ, modifiedBy)
                .addExpressOfBetweenOrEq(!ObjectUtils.isEmpty(modifiedDate), "modifiedDate", modifiedDate)
                .build();
    }

    @Override
    public List<QueryExpress> filterCriterion() {

        // 需要自行根据索引调整可支持的通用查询字段，切不可全字段通用查询
        List<QueryExpress> filterBys = super.filterCriterion();
        if (!StringUtils.hasLength(filter)) {
            /*filterBys.add(new Express("createdByName}", Operator.LIKE, filter.trim()));*/
        }
        return filterBys;
    }
}
